/*

 * Copyright (c) 2014, Oracle America, Inc.

 * All rights reserved.

 *

 * Redistribution and use in source and binary forms, with or without

 * modification, are permitted provided that the following conditions are met:

 *

 *  * Redistributions of source code must retain the above copyright notice,

 *    this list of conditions and the following disclaimer.

 *

 *  * Redistributions in binary form must reproduce the above copyright

 *    notice, this list of conditions and the following disclaimer in the

 *    documentation and/or other materials provided with the distribution.

 *

 *  * Neither the name of Oracle nor the names of its contributors may be used

 *    to endorse or promote products derived from this software without

 *    specific prior written permission.

 *

 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"

 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE

 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE

 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE

 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR

 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF

 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS

 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN

 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)

 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF

 * THE POSSIBILITY OF SUCH DAMAGE.

 */

package org.openjdk.jmh.samples;


import org.openjdk.jmh.annotations.*;

import org.openjdk.jmh.runner.Runner;

import org.openjdk.jmh.runner.RunnerException;

import org.openjdk.jmh.runner.options.Options;

import org.openjdk.jmh.runner.options.OptionsBuilder;


import java.util.concurrent.*;


/**
 * Fixtures have different Levels to control when they are about to run.
 * <p>
 * Level.Invocation is useful sometimes to do some per-invocation work
 * <p>
 * which should not count as payload (e.g. sleep for some time to emulate
 * <p>
 * think time)
 */

@OutputTimeUnit(TimeUnit.MICROSECONDS)

public class JMHSample_07_FixtureLevelInvocation {


    /*

     * Fixtures have different Levels to control when they are about to run.

     * Level.Invocation is useful sometimes to do some per-invocation work,

     * which should not count as payload. PLEASE NOTE the timestamping and

     * synchronization for Level.Invocation helpers might significantly offset

     * the measurement, use with care. See Level.Invocation javadoc for further

     * discussion.

     *

     * Consider this sample:

     */


    /*

     * This state handles the executor.

     * Note we create and shutdown executor with Level.Trial, so

     * it is kept around the same across all iterations.

     */


    @State(Scope.Benchmark)

    public static class NormalState {

        ExecutorService service;


        @Setup(Level.Trial)

        public void up() {

            service = Executors.newCachedThreadPool();

        }


        @TearDown(Level.Trial)

        public void down() {

            service.shutdown();

        }


    }


    /*

     * This is the *extension* of the basic state, which also

     * has the Level.Invocation fixture method, sleeping for some time.

     */


    public static class LaggingState extends NormalState {

        public static final int SLEEP_TIME = Integer.getInteger("sleepTime", 10);


        @Setup(Level.Invocation)

        public void lag() throws InterruptedException {

            TimeUnit.MILLISECONDS.sleep(SLEEP_TIME);

        }

    }


    /*

     * This allows us to formulate the task: measure the task turnaround in

     * "hot" mode when we are not sleeping between the submits, and "cold" mode,

     * when we are sleeping.

     */


    @Benchmark

    @BenchmarkMode(Mode.AverageTime)

    public double measureHot(NormalState e, final Scratch s) throws ExecutionException, InterruptedException {

        return e.service.submit(new Task(s)).get();

    }


    @Benchmark

    @BenchmarkMode(Mode.AverageTime)

    public double measureCold(LaggingState e, final Scratch s) throws ExecutionException, InterruptedException {

        return e.service.submit(new Task(s)).get();

    }


    /*

     * This is our scratch state which will handle the work.

     */


    @State(Scope.Thread)

    public static class Scratch {

        private double p;

        public double doWork() {

            p = Math.log(p);

            return p;

        }

    }


    public static class Task implements Callable<Double> {

        private Scratch s;


        public Task(Scratch s) {

            this.s = s;

        }


        @Override

        public Double call() {

            return s.doWork();

        }

    }


    /*

     * ============================== HOW TO RUN THIS TEST: ====================================

     *

     * You can see the cold scenario is running longer, because we pay for

     * thread wakeups.

     *

     * You can run this test:

     *

     * a) Via the command line:

     *    $ mvn clean install

     *    $ java -jar target/benchmarks.jar JMHSample_07 -f 1

     *    (we requested single fork; there are also other options, see -h)

     *

     * b) Via the Java API:

     *    (see the JMH homepage for possible caveats when running from IDE:

     *      http://openjdk.java.net/projects/code-tools/jmh/)

     */


    public static void main(String[] args) throws RunnerException {

        Options opt = new OptionsBuilder()

                .include(JMHSample_07_FixtureLevelInvocation.class.getSimpleName())

                .forks(1)

                .build();


        new Runner(opt).run();

    }

// 本例中主要介绍了使用Level.Invocation达到每次方法执行完成后sleep一段时间，模拟在需要唤醒线程的情况下耗时更多。
}